<?php
/**
* PASSWORDと権限管理ククラス
* 共通関数などもあります
**/

class Class_FUNC{
	public static $_arr_weeks = array( "日", "月", "火", "水", "木", "金", "土"); 
	public static function do_encode($value ){	
		//return md5($value);
		return $value;
	}
	
	public static function do_decode($value ){	
		//return md5($value);
		return $value;
	}
	//
	public static function valid_auth($val, $auth = NULL ){	
			$aoki_auth = $_SESSION['ADMIN_LOGIN']['STAFF_AUTH_ARR'];
			if ( !isset( $aoki_auth[$val]) ) {
				echo ("メニューにアクセス不可です。");
				exit;
			}
			$url = $_SERVER['PHP_SELF'];
			$arr = explode( '/' , $url );
			$filename= $arr[count($arr)-1];
			$filename = str_replace(".php","",$filename);
	}
	//前年$matrix_cd
	public static function get_pre_matrix_cd( $matrix_cd ){
		return (substr($matrix_cd,0,4)* 1 -1).substr($matrix_cd,4);
	}
	
	//yyyymmddからyyyy/mm/dd変換
	public static function int8_to_date($val ){
		if (empty($val)) {
			return "";
		}
		return date("Y/m/d", strtotime($val));
	}
	//y/n/j
	public static function int8_to_date_nozero($val ){
		if (empty($val)) {
			return "";
		}
		return date("Y/n/j", strtotime($val));
	}
	
	//yyyymmddからyyyy/mm/dd(D)変換
	public static function int8_to_dateD($val ){
		if (empty($val)) {
			return "";
		}
		$shortime =  strtotime($val);
		return date("Y/m/d",$shortime)."(".Class_PWD::$_arr_weeks[date( "w",$shortime)].")";
		
	}
		//yyyymmddからyyyy/mm/dd(D)変換
	public static function int8_to_cell_dateD($val ){
		if (empty($val)) {
			return "";
		}
		$shortime =  strtotime($val);
		return date("m/d",$shortime)."(".Class_PWD::$_arr_weeks[date( "w",$shortime)].")";
	}
	//yyyymmddか変換
	public static function dateTime_to_dateD($val ,&$time = null){
		if (empty($val)) {
			return "";
		}
		$shortime =  strtotime($val);
		if (!empty($time)) {
			$time  = date("H",$shortime);
		} 
		
		return date("Y/m/d",$shortime)."(".Class_PWD::$_arr_weeks[date( "w",$shortime)].")";
	}
		//yyyy-mm-dd 00:00:00か変換Y-m-d H:i:s
	public static function dateTime_to_YmdHi($val ){
		if (empty($val)) {
			return "";
		}
		$shortime =  strtotime($val);
		
		return date("Y/m/d H:i",$shortime);
	}
	//yyyy/mm/ddからyyyymmdd変換
	public static function date_to_int8($str ){
		if (empty($str)) {
			return 0;
		}
		return date('Ymd', strtotime($str));
	}
	
	//yyyy/mm/dd(d)からyyyymmdd変換
	public static function dateD_to_int8($str ){
		if (empty($str)) {
			return 0;
		}
		
		return date('Ymd', strtotime($str));
	}
	
	public static function dateHH_to_time($str, $hour ){
		if ( empty($str) ) {
			return NULL;
		}
		if  ( empty($hour) ){
			return substr($str,0,10)." 00:00:00";
		} else {
			return substr($str,0,10)." ".$hour.":00:00";
		}
		
	}
	
	
	//年プルダウン配列
	public static function get_years( ){
		$year4 = array();
		$nowYear = date('Y');
		for( $a =$nowYear ; $a >=$nowYear - 90; $a-- )
		{
			$year4[$a] = $a;
		}
		return $year4;
	}
	
		//月プルダウン配列
    public static function get_moons( ){
		$moon4 = array('01'=>'01','02'=>'02','03'=>'03','04'=>'04','05'=>'05', '06'=>'06','07'=>'07','08'=>'08','09'=>'09','10'=>'10','11'=>'11','12'=>'12');
		return $moon4;
	}

	//$intime 日付yyyy/mm/dd
	//$flag  1: >=,  2:<=
	public static function get_u_time_where_sql($intime, $flag = 1,$table =null ){
		
		$the_time = str_replace( '/', '-', $intime);
		if ($flag==1) {
			$the_time = $the_time. " 00:00:00";
			if ($table) {
				return  $table.".u_time >=  '{$the_time}'";
			} else {
				return  " u_time >=  '{$the_time}'";
			}
		}
		 
		if ($flag==2) {
			$the_time = $the_time. " 23:59:59";
			if ($table) {
				return  $table.".u_time <=  '{$the_time}'";
			}else {
				return  " u_time <=  '{$the_time}'";
			}
		}
		
	}
	
	public static function dateTime_to_YmdHis($val ){
		if (empty($val)) {
			return "";
		}
		$shortime =  strtotime($val);
		return date("Y/m/d H:i:s",$shortime);
	}
	

	
	public static function  set_rate_inc(&$values){		
		//計画比   計画差異
		$values["plane_inc"] =  $values["now_cost"] - $values["plane_cost"];
		if ( !empty($values["plane_cost"]) ) {
			$values["plane_rate"] =  $values["now_cost"] / $values["plane_cost"] * 100;
		} else {
			
		}
		
		//前年比   前年差異
		$values["last_inc"] =  $values["now_cost"] - $values["last_cost"];
		if ( !empty($values["last_cost"]) ) {
			$values["last_rate"] =  $values["now_cost"] / $values["last_cost"] * 100;
		}else {
			
		}		
	}
	
	public static function get_goodsset(){
		require_once( '../system/ecjp_m.php' );
	    require_once( '../system/mdao/t_m_goodsset.php' );
	    $goodsset = new t_m_goodsset;
	    $data = $goodsset->search();
	    foreach( $data as $col => $val ) {
		 $data1[$val['category']] = $val;
        }
	    
	     return $data1;
	    
	}
	
	public static function senduserMail($smtpemailto,$subject, $content) {

		require_once ('../system/PHPMailer/class.phpmailer.php');
		mb_language('japanese');
		mb_internal_encoding('UTF-8');
		$mail = new PHPMailer();
		    $mail->IsSMTP();
		    $mail->Host = 'mail.163.com';
		    $mail->SMTPAuth = true; 
		 	$mail->CharSet = 'UTF-8';
			$mail->Encoding = '7bit';
			$mail->Username   =  'syqupeng10@163.com';   // MAIL username
			$mail->Password   =  'asdasd1';      // MAIL password
			$mail->Port = '25';
			$mail->SetFrom('syqupeng10@163.com',"PREGO");
			$mail->Subject = mb_encode_mimeheader( $subject, 'JIS');		
			$mail->Body  = mb_convert_encoding( $content, 'JIS', 'UTF-8');
			$mail->AddAddress($smtpemailto);
			$mail->Send();	
		    return 	$mail->Send();
		    	
	}
	    //$goods_id,$image_name=null,$size=0,$width=0,$hight=0
     public static function get_imagepath($goods_id,$image_names=null,$size=0,$width=0,$height=0){
		//return md5($value);
		$img_url = "images/{$goods_id}/";
			$image_namearr = unserialize($image_names); 
			$image_name = "images/none.jpg";
			if (count($image_namearr)>0){
				$image_name = $img_url.$image_namearr[0];
			}
			if ($size==1) {
				return "imgg.php?id={$goods_id}&img=".$image_name."&s=0-130";
			}
			if ($size==2) {
				return "imgg.php?id={$goods_id}&img=".$image_name."&s=0-300";
			}
			if ($size==3) {
				return "imgg.php?id={$goods_id}&img=".$image_name."&s=0-50";
			}
			if ($size==6) {
				return "imgg.php?id={$goods_id}&img=".$image_name."&s=0-600";
			}
			
			if ($size==101) {
				return "imgg.php?id={$goods_id}&img=".$image_name."&s=0-80";
			}
			if ($size==102) {
				return "imgg.php?id={$goods_id}&img=".$image_name."&s=0-130";
			}
			if ($size==103) {
				return "imgg.php?id={$goods_id}&img=".$image_name."&s=0-30";
			}
			if ($size==106) {
				return "imgg.php?id={$goods_id}&img=".$image_name."&s=0-200";
			}
			if  (($width>0) or ($height>0)) {
				return "imgg.php?id={$goods_id}&img=".$image_name."&s={$width}-{$height}";
			}else{
				return "imgg.php?id={$goods_id}&img=".$image_name ;
			}
			
        	
        /*
		  if($size !=0){
		  	    $image = unserialize($image_names);	
		  	    if(is_array($image))
		  	      {	 
		  	      	
		  	      	$size = $size-1;
		  	      	if(isset($image[$size]))
		            $path = "./images/$goods_id/$image[$size]";
		  	      }
		  } else {
		  	    $image = unserialize($image_names);		    
		        $path = "images/none.jpg";
		  }
        
	       return $path;
	       
	        */
        
	 }
	 
/* $status説明
	    user_insert =  ユーザ登録
        user_update =  ユーザ更新
        order_over  =  注文完了
     */
   public static function get_email($status,$site_id = 1)
   {
        $db = new Class_DB;
        $sql = sprintf("select * from m_site_email where site_id ='".$site_id."'  and status = '".$status ."'");

        $db->query($sql);
        
        
		if( $row = $db->fetch() ){
			$mail_file = "mail/{$site_id}/".$status.".txt";
			$row["tempfile"] = $mail_file;
			return $row;
		}
		return null;
               
    }
    
	public static function sendMail($mailobj,$tomail) {

			require_once ('../system/PHPMailer/class.phpmailer.php');
			mb_language('japanese');
			mb_internal_encoding('UTF-8');
			//$mail->Mailer = "sendmail";
		
			//$mail->IsHTML(true);
			//$mail->Host       = PREGO_SMTP_SERVER; // SMTP server
			//$mail->IsSMTP();
			//$mail->SMTPAuth   = true;                  // enable SMTP authentication
			//$mail->SMTPDebug  = 2;
			$mail = new PHPMailer();
		 	$mail->CharSet = 'iso-2022-jp';
			$mail->Encoding = '7bit';
			$mail->Username   =  $mailobj["fromemail"];   // MAIL username
			$mail->SetFrom($mailobj["fromemail"], $mailobj["emailname"]);
			//$mail->Password   =  PREGO_SMTP_PASS;      // MAIL password
			$mail->Port = '25';
	
			$mail->AddReplyTo($mailobj["reply"], $mailobj["reply"]);
			
			
			$mail->Subject = mb_encode_mimeheader( $mailobj["letter_title"], 'JIS');		
			$mail->Body    = mb_convert_encoding( $mailobj["letter_content"], 'JIS', 'UTF-8');
			$mail->AddAddress($tomail);
			$mail->Send();	
			
		    return 	$mail->Send();
		    	
	}
	 public static function get_site($site=1){
	    require_once( '../system/mdao/t_m_siteconfig.php' );
	    $siteconfig = new t_m_siteconfig;
	    $data = $siteconfig->get($site);	    
	    return  $data;
	 }

	
	 
}

?>